How to implement backup & restore in Android app 您所在的位置:网站首页 restore user data How to implement backup & restore in Android app

How to implement backup & restore in Android app

2024-07-04 23:26| 来源: 网络整理| 查看: 265

Exploring how to provide your own backup-restore mechanism in your Android appSpians Labs

Spians Labs

Follow

5 min read路Jun 7, 2020

--

Photo by Markus Winkler on Unsplash

Backup & Restore is infrequent yet very important functionality for Android apps. It becomes even more critical when an App is not using its own dedicated cloud storage to store user data. As a developer, when you are making heavy use of local storage, it鈥檚 important that your app starts back with same experience when reinstall happens. Android comes with its own backup support but not without some problems. Here are some of the problems with in built backup support.

Size limit : Android鈥檚 built in backup option only supports backups till 25 MB in size. While this might be enough for most apps, it might not be enough for all apps. Apps with huge local storage can not use this.Password protected / encrypted backups: Password protected backups is not possible with default option. Privacy sensitive apps might want to provide this option to users. Encrypted backups is only possible on version N and above. Note that creating password protected backup is different than encrypting content of backup. Having a password of backup adds extra layer of security because you can combine user given password with random key to block any attempt to open the backup file.Restoration: Restoration only happens during setup wizard or when the app is reinstalled. Users can鈥檛 restore data from within the app anytime they want.Minimum API level: Your app requires minimum API level 23. Users below that will not be able to use it.Storage preference: Users might not always want to backup their data on Google drive. Giving them capability to store wherever they want is definitely a huge plus point.Bugs: Bugs like these will be out of your control and your users might suffer because of this.

Because of all these problems, I decided to provide my own implementation of Backup & Restore in my App. Here鈥檚 how I did it.

Theoretically it鈥檚 pretty simple. If you take a snapshot of your app鈥檚 data directory while creating a backup and restore content of data directory from that snapshot later, your app will be in the exact same state as when that snapshot was taken. Here are the steps that we would need to follow

BackupCreate a password protected .zip file in external storage.Add all the data directories to that .zip file (that we want to include in backup)RestoreExtract the password protected .zip filePut back all the directories inside data directory of the app

Let鈥檚 go steps by steps to see how you can create backup and restore it later.

Fire ACTION_CREATE_DOCUMENT intent to create a file in external storage. In onActivityResult you get the URI of the file that is about to get created. You can write to this file and this will be our final backup zip file.

And then in your onActivityResult

Look at the function packZipFileForBackup . It does all the work of creating our file backup file. Let鈥檚 look at the implementation of that function.

2. Before we look at the implementation, one important thing to note here is that Java does not have native support for password protected zip files. We鈥檒l use this nice library called Zip4j to make our zips password protected. Now here is the implementation of the function:

Declare a file with the path you want which will be our final zip backup filePass this file to Zip4j as a final backup file and add all the data to itZip4j will pack a zip file for us which will be our backup fileWe鈥檒l copy entire directories (that we want to include in backup) in our zip file. This will be helpful when restoring as we can just put these directories back into data directory so that we don鈥檛 have to worry about file level details. Here, we鈥檒l copy Shared preference, files and database directories in our zip file. You can add any custom directory that you might have created for your app in the zip file.

Look at this snippet. This basically tells Zip4j to encrypt zip files and make zip password protected.

val encZipFile = ZipFile(zipFile.absolutePath, "password".toCharArray())val zipParameters = ZipParameters() zipParameters.isEncryptFiles = true zipParameters.encryptionMethod = EncryptionMethod.AES

3. Restoring is exactly the reverse of backup as you鈥檇 imagine. We pass the password that was set for this zip and ask Zip4j to extract all the files at a temporary location. Now because we know every folder inside the zip file is just a direct child of the data directory, we just recursively copy all the folder inside zip and put them under data directory as it is without having to worry about names and their location. Let鈥檚 see the code.

You can fire an intent again to select the zip file residing in external storage like this:

And then in your onActivityResult , for above request code:

restoreFromInputStream does the actual restoration of the data. Let鈥檚 look at implementation of the function:

Copy stream into temporary fileCreate a temporary directory and extract the zip file content into it. At this point, Zip4j will throw ZipException with type WRONG_PASSWORDDelete existing files and directories of Shared Preferences, files and Database directories.Put all the extracted directories from zip file in data folder as it is.Cleanup temporary zip file and directory used at the end.In case of wrong password, IncorrectPasswordException is being thrown here which is a custom exception. You can opt out of custom exception here and propagate ZipException if you want.

One important thing to note here is that after restoring all the directories, You鈥檒l have to re create your db instance (SQLiteDatabase instance). Also, you鈥檒l have save all the preferences (in restored preference file) by using SharedPreferences editor API to reflect those changes in current process. I鈥檒l leave that as an exercise for you. I have used coroutines to switch to IO dispatchers in my implementation, but you can RxJava or any other framework that you are using in your project.

I鈥檝e implemented this in my app Plenary. It鈥檚 an RSS reader app with heavy offline support (such as downloading entire articles in your local storage). Since there is no cloud storage for app, it was critical to provide options to backup data on user鈥檚 preferred storage. I have provided options to create backups in local storage (covered in this article), Google drive and Dropbox. Creating backups and restoring process is same for any storage. For example, packZipFileForBackup() and restoreFromStream() are also being used for Google drive and Dropbox.

That鈥檚 it! Let me know the feedback in responses section.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有